<?php

class Eventos extends Zend_Db_Table_Abstract
{

    protected $_name = 'evento';
    protected $_referenceMap = array(
        'Metaevento' => array(
            'columns' => array('metaevento_id'),
            'refTableClass' => 'Metaevento',
            'refColumns' => array('id')
        ),
        'Moodle' => array(
            'columns' => array('mdl_curso_id'),
            'refTableClass' => 'mdlCursos',
            'refColumns' => array('id')
        ),
    );
    
    public function excluirEvento($metaevento_id, $mdl_curso_id){
        $where = $this->getAdapter()->quoteInto('metaevento_id = ?', $metaevento_id)
              . $this->getAdapter()->quoteInto(' and mdl_curso_id=?',$mdl_curso_id);
        
        $this->delete($where);
    }
    
    public function excluirTodosEventos($metaevento_id){
        $metaevento_id = explode('=',$metaevento_id);
        $where = 'metaevento_id = '.$metaevento_id[1];
        $this->delete($where);
    }
    
    public function listaEventosOrfaos($nome = null){
            $select_metaevento = $this->getAdapter()->select()
                ->from(array('e' => 'gesac.evento'),array('id'=>'mdl_curso_id'));

            $select = $this->getAdapter()->select();
            $select->from(array('c' => 'moodle.mdl_course'),array(
                'id',
                'fullname'
            ))
                ->join(array('ct'=> 'moodle.mdl_course_categories'),'ct.id=c.category', array(
                    'categoria'=>'name'
                ))
                ->where('c.id not in ?',$select_metaevento);
            if(!null==$nome){
                $select->where("c.fullname like '%".$nome."%' ");
            }
            $select->order('ct.name','c.fullname');
            
            $teste = $select->__toString();
            return $select->query()->fetchAll();
    }
    
    public static function getEventos($where = null, $order = null)
    {
        $db        = Zend_Db_Table::getDefaultAdapter();
        $cols      = array('e.*');
        $evento    = array('e' => 'evento');
        $usuario   = array('u' => 'usuario');
        
        $select = $db->select()
                     ->from($evento, $cols)
                     ->joinLeft($usuario, 'u.id = e.responsavel_id',array('u.apelido as responsavel'));
        if($where){$select->where($where);}
        if($order){$select->order($order);}

        $result = $db->fetchAll($select);
        
        return $result;
        
    }
    
}